<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-5-19
 * Time: 上午11:54
 */

namespace ActionsHttp;

use Data\Food;
use Data\Food_DAO;
use Data\Food_groups_DAO;
use Data\Food_tags_DAO;

class RestaurantFoodAdd extends BaseActionHttp
{
    public function doAction() {
        parent::doAction();

        // 店铺id
        $restaurant_id = $_SESSION[RESTAURANT_ID];
        $restaurant_id = 1;

        // 商品分类id
        $food_groups_id = (int)$this->data[FOOD_GROUP_ID];
        $food_groups_record = Food_groups_DAO::getRecordByIdResId($food_groups_id,$restaurant_id);
        if (!$food_groups_record) {
            return $this->SendErrMsg("商品分类参数错误");
        }

        // 商品标签
        $food_tag_id = $this->data[FOOD_TAG_ID];
        $food_tag_record = Food_tags_DAO::getRecord($food_tag_id);
        if (!($food_tag_record && $food_tag_record->getRESTAURANT_ID() == $restaurant_id)) {
            return $this->SendErrMsg("商品标签参数错误");
        }

        // 商品名称
        $name = $this->data[FOOD_NAME];
        // 商品图片
        $img_url = $this->data[IMG_URL];
        // 价格
        $price = $this->data[FOOD_PRICE];
        // 是否限量
        $is_limit = (int)$this->data[IS_LIMIT];
        // 餐盒数量
        $lunchbox_amounts = (int)$this->data[LUNCHBOX_AMOUNTS];
        // 餐盒价格
        $lunchbox_price = $this->data[LUNCHBOX_PRICE];
        // 是否在售
        $is_on_sell = (int)$this->data[IS_ON_SELL];
        // 每日库存
        $stock_day = (int)$this->data[STOCK_DAY];
        // 剩余库存
        $stock_left = (int)$this->data[STOCK_LEFT];
        // 商品特色
        $special_feature = (int)$this->data[SPECIAL_FEATURE];
        // 单位
        $order_unit = $this->data[ORDER_UNIT];
        // 最小购买数量
        $order_minimum = (int)$this->data[ORDER_MINIMUM];
        // 商品属性
        $food_property = $this->data[FOOD_PROPERTY];
        // 描述
        $description = $this->data[DESCRIPTION];
        // 操作人
        $created_by = $restaurant_id;


        // 新建
        $food_record = new Food(array());
        $food_record->setFOOD_GROUP_ID($food_groups_id);
        $food_record->setFOOD_TAG_ID($food_tag_id);
        $food_record->setFOOD_NAME($name);
        $food_record->setDESCRIPTION($description);

        $food_record->setIMG_URL($img_url);
        $food_record->setFOOD_PRICE($price);
        $food_record->setIS_LIMIT($is_limit);
        $food_record->setLUNCHBOX_AMOUNTS($lunchbox_amounts);
        $food_record->setLUNCHBOX_PRICE($lunchbox_price);
        $food_record->setIS_ON_SELL($is_on_sell);
        $food_record->setSTOCK_DAY($stock_day);
        $food_record->setSTOCK_LEFT($stock_left);
        $food_record->setSPECIAL_FEATURE($special_feature);
        $food_record->setORDER_UNIT($order_unit);
        $food_record->setORDER_MINIMUM($order_minimum);

        $food_record->setCREATED_BY($created_by);

        $food_record->setRESTAURANT_ID($restaurant_id);

        // 插入数据库
        Food_DAO::insert($food_record);


        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}